import poplib
import email
import time
from email.parser import Parser
from email.header import decode_header

#字符编码转换
def decode_str(s):
    value, charset = decode_header(s)[0]
    if charset:
        value = value.decode(charset)
    return value

def get_att(msg):
    attachment_files = []

    for part in msg.walk():
        # 获取附件名称类型
        file_name = part.get_filename()  
        contType = part.get_content_type()

        if file_name:
            h = email.header.Header(file_name)
            # 对附件名称进行解码
            dh = email.header.decode_header(h)  
            filename = dh[0][0]
            if dh[0][1]:
                # 将附件名称可读化
                filename = decode_str(str(filename, dh[0][1]))  
                print(filename)
                # filename = filename.encode("utf-8")
            # 下载附件
            data = part.get_payload(decode=True)
            # 在指定目录下创建文件，注意二进制文件需要用wb模式打开
            att_file = open('D:\\scan\\' + filename, 'wb')  
            attachment_files.append(filename)
            # 保存附件
            att_file.write(data)  
            att_file.close()
    return attachment_files

# 读取配置文件  
with open('D:\\config.cfg', 'r') as f1:
    config = f1.readlines()
for i in range(0, len(config)):
    config[i] = config[i].rstrip('\n')

# POP3服务器、用户名、密码
host = config[0]  # POP3服务器
username = config[1]  # 用户名 
password = config[2]  # 密码

# 连接到POP3服务器
server = poplib.POP3(host)

# 身份验证
server.user(username)
server.pass_(password)

# stat()返回邮件数量和占用空间:
# print('Messages: %s. Size: %s' % server.stat())

# 可以查看返回的列表类似[b'1 82923', b'2 2184', ...]
resp, mails, octets = server.list()
# print(mails)

# 下载所有附件
# index = len(mails)
# for i in range(index, 0, -1):
#     # lines存储了邮件的原始文本的每一行
#     resp, lines, octets = server.retr(i)

#     # 邮件的原始文本:
#     msg_content = b'\r\n'.join(lines).decode('utf-8')

#     # 解析邮件:
#     msg = Parser().parsestr(msg_content)

#     # 获取附件
#     f_list = get_att(msg)

# 获取最新一封邮件, 注意索引号从1开始:
index = len(mails)
resp, lines, octets = server.retr(index)
# lines存储了邮件的原始文本的每一行,
# 可以获得整个邮件的原始文本:
msg_content = b'\r\n'.join(lines).decode('utf-8')
# 稍后解析出邮件:
msg = Parser().parsestr(msg_content)
# 获取附件
f_list = get_att(msg)

# 关闭连接:
server.quit()
print("文件已下载完成，5秒后关闭程序！")
time.sleep(5)